<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="128px"
    >
      <el-form-item label="检索类型" prop="senderType">
        <el-select
          v-model="queryParams.searchType"
          placeholder="检索类型"
          clearable
          class="!w-240px"
        >
          <el-option label="数据库检索" :value="1" />
          <el-option label="向量检索" :value="2" />
        </el-select>
      </el-form-item>
      <template v-if="queryParams.searchType === EnumQueryType.DATABASE">
<!--        <el-form-item label="角色id" prop="roleId">-->
<!--          <el-input-->
<!--            v-model="queryParams.roleId"-->
<!--            placeholder="请输入角色id"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--            class="!w-240px"-->
<!--          />-->
<!--        </el-form-item>-->
        <el-form-item label="消息来源" prop="senderType">
          <el-select
            v-model="queryParams.senderType"
            placeholder="消息来源"
            clearable
            class="!w-240px"
          >
            <el-option label="AI回复" :value="2" />
            <el-option label="用户" :value="1" />
            <el-option label="关键词回复" :value="3" />
            <el-option label="向量录入" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户消息" prop="userText">
          <el-input
            v-model="queryParams.userText"
            placeholder="请输入用户消息"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="回复消息" prop="replyText">
          <el-input
            v-model="queryParams.replyText"
            placeholder="请输入回复消息"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
          />
        </el-form-item>
      </template>
      <template v-if="queryParams.searchType === EnumQueryType.VECTOR">
        <el-form-item label="相似度" prop="similarity">
          <el-input-number
            :min="1"
            :max="100"
            :step="1"
            v-model="queryParams.similarity"
            placeholder="请输入相似度"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="用户消息" prop="userText">
          <el-input
            v-model="queryParams.userText"
            placeholder="请输入用户消息"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="回复消息" prop="replyText">
          <el-input
            v-model="queryParams.replyText"
            placeholder="请输入回复消息"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
      </template>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" />{{queryParams.searchType===EnumQueryType.DATABASE ? "数据库检索" : "向量检索"}}</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['lm:message-collection:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['lm:message-collection:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column width="250px" label="消息id" align="center" prop="messageId" />
<!--      <el-table-column width="80px" label="角色id" align="center" prop="roleId" />-->
      <el-table-column width="150px" label="消息来源" align="center" prop="senderType" >
        <template #default="scope">
          <span v-if="scope.row.senderType === 1">用户</span>
          <span v-if="scope.row.senderType === 2">AI回复</span>
          <span v-if="scope.row.senderType === 3">关键词回复</span>
          <span v-if="scope.row.senderType === 4">向量录入</span>
        </template>
      </el-table-column>
      <el-table-column width="240px" label="用户消息" align="center" prop="userText" />
      <el-table-column label="回复消息" align="center" prop="replyText" />
      <el-table-column label="相似度" align="center" prop="similarity" >
        <template #default="scope">
          <span v-if="scope.row.similarity">
            {{scope.row.similarity * 100}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column width="140px" label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.messageId)"
            v-hasPermi="['lm:message-collection:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.messageId)"
            v-hasPermi="['lm:message-collection:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MessageCollectionForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { MessageCollectionApi, MessageCollectionVO } from '@/api/lm/messagecollection'
import MessageCollectionForm from './MessageCollectionForm.vue'

/** 消息仓库 列表 */
defineOptions({ name: 'MessageCollection' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<MessageCollectionVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const EnumQueryType = {
  DATABASE: 1,
  VECTOR: 2
};
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  roleId: undefined,
  searchType: EnumQueryType.DATABASE,
  senderType: undefined,
  similarity: 80,
  userText: undefined,
  replyText: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    let params = {
      ...queryParams
    };
    switch(params.searchType) {
      case EnumQueryType.DATABASE:
        delete params.similarity;
        break;
      case EnumQueryType.VECTOR:
        delete params.roleId;
        delete params.senderType;
        delete params.createTime;
        break;
    }
    const data = await MessageCollectionApi.getMessageCollectionPage(params)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.searchType = EnumQueryType.DATABASE;
  queryParams.similarity = 80;
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MessageCollectionApi.deleteMessageCollection(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MessageCollectionApi.exportMessageCollection(queryParams)
    download.excel(data, '消息仓库.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
